These samples demonstrate some ways to use LINQ to XML to join, filter, order, search, etc.

[Category("XMP - Experiences and Exemplars")]
[Description(@"List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order.")]
public void Q7()
{
XDocument bib = XDocument.Load("bib.xml");
var result = new XElement("bib",
from b in bib.Descendants("book")
where (string)b.Element("publisher") == "Addison-Wesley" &&
(int)b.Attribute("year") > 1991
orderby (string)b.Element("title")
select new XElement("book",
b.Attribute("year"),
b.Element("title")));
Console.WriteLine(result);
// Solution in XQuery:
// <bib>
// {
// for $b in doc(""http://bstore1.example.com/bib.xml"")//book
// where $b/publisher = ""Addison-Wesley"" and $b/@year > 1991
// order by $b/title
// return
// <book>
// { $b/@year }
// { $b/title }
// </book>
// }
// </bib>
}
[Category("XMP - Experiences and Exemplars")] [Description(@"List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order.")] public void Q7() { XDocument bib = XDocument.Load("bib.xml"); var result = new XElement("bib", from b in bib.Descendants("book") where (string)b.Element("publisher") == "Addison-Wesley" && (int)b.Attribute("year") > 1991 orderby (string)b.Element("title") select new XElement("book", b.Attribute("year"), b.Element("title"))); Console.WriteLine(result); // Solution in XQuery: // <bib> // { // for $b in doc(""http://bstore1.example.com/bib.xml"")//book // where $b/publisher = ""Addison-Wesley"" and $b/@year > 1991 // order by $b/title // return // <book> // { $b/@year } // { $b/title } // </book> // } // </bib> }
For more information on LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx